<?php
	namespace ampleharvest\webpages\rest\admin;

	use \ampleharvest\cConfig;
	use \ampleharvest\cServers;
	use \ampleharvest\permissions;
	use \ampleharvest\code\cRestConnector;

	require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/config/cConfig.php";
	require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/config/cServers.php";
	
	$webConfig= new \ampleharvest\cConfig();
	
	require_once $webConfig->C_STR_AMPLEHARVEST_CODE . "/classes/User_Manager/cUserValidation.php";
	require_once $webConfig->C_STR_AMPLEHARVEST_CODE . "/config/cPermissions.php";
	require_once $webConfig->C_STR_AMPLEHARVEST_CODE . "/abstract_classes/abs_RestService.php";
	require_once $webConfig->C_STR_AMPLEHARVEST_CODE . '/classes/cStomp.php';
	require_once $webConfig->C_STR_AMPLEHARVEST_CODE . "/classes/search/solr/cSolr.php";
	require_once $webConfig->C_STR_AMPLEHARVEST_CODE . '/classes/cRestConnector.php';

	class cStatus extends \abs_RestService
	{
		protected $m_obj_UserValidation = null;

		public function __construct()
		{
			$this->m_obj_UserValidation = new \cUserValidation();

			$this->m_obj_UserValidation->require_Login();

			$this->m_obj_TABLE_Users = new \cTABLE_Users();

			parent::__construct();
		}

		public function output_Get_Info()
		{
			print("<h1>GET</h1>");
			print("<p>Returns the status of the components in Json format</p>");
			print("<h4>Parameters</h4><p>none</p>");
		}

		public function get_Function($arr_Values)
		{
			$arr_Output = array();

			$bool_ViewStatus = $this->m_obj_UserValidation->check_Permission(permissions\cPermissions::C_STR_PERMISION_ADMIN_VIEW_STATUS);

			if (!$bool_ViewStatus)
			{
				$this->output_Error("You do not have permission to view this page.");
			}
			//ActiveMQ
			$arr_Output[] = $this->check_ActiveMQ();
			//AmpleHarvestDB
			$arr_Output[] = $this->check_AmpleHarvestDB();
			//AmpleSilo
			$arr_Output[] = $this->check_AmpleSilo();
			//FileRepo
			$arr_Output[] = $this->check_FileRepo();
			//SOLR
			$arr_Output[] = $this->check_SOLR();
			//User Manager
			$arr_Output[] = $this->check_User_Manager();

			$this->output_Data($arr_Output);
		}

		protected function check_SOLR()
		{
			$obj_Output = new \stdClass();
			$obj_Output->status = "UP";

			$obj_Output->name = "SOLR";
			$obj_Output->date = date_format(new \DateTime(), "c");

			try
			{
				$obj_Solr = new \cSolr(null, null, null);
				$this->output_Error("Using default solr instance -- status.php");
			}
			catch (\Exception $e)
			{
				$obj_Output->status = "DOWN";
				$obj_Output->msg = $e->getMessage();
			}

			return $obj_Output;
		}

		protected function check_ActiveMQ()
		{
			$obj_Output = new \stdClass();
			$obj_Output->status = "UP";

			$obj_Output->name = "ActiveMQ";
			$obj_Output->date = date_format(new \DateTime(), "c");

			try
			{
				$obj_Stomp = new \cStomp();
			}
			catch (\Exception $e)
			{
				$obj_Output->status = "DOWN";
				$obj_Output->msg = $e->getMessage();
			}

			return $obj_Output;
		}

		protected function check_AmpleSilo()
		{

			$obj_Output = new \stdClass();
			$obj_Output->status = "UP";
			$obj_Output->name = "AmpleSilo";
			$obj_Output->date = date_format(new \DateTime(), "c");

			$str_URL = self::serverLookup("F535ACC4-3CD9-467D-8FC7-B8C920C40378");

			if ($str_URL == "")
			{
				$obj_Output->status = "DOWN";
				$obj_Output->msg = "AmpleSilo status url is not set (F535ACC4-3CD9-467D-8FC7-B8C920C40378)";
			}
			else
			{
				$obj_Rest = new cRestConnector($str_URL);

				$str_Return = $obj_Rest->send_Get();
				if (!$str_Return)
				{
					$obj_Output->status = "DOWN";
					$obj_Output->msg = $obj_Rest->getLastError();

				}
				else 
				{
					$obj_Output->msg = $str_Return;
				}
			}
			return $obj_Output;
		}
		
	protected function check_FileRepo()
		{

			$obj_Output = new \stdClass();
			$obj_Output->status = "UP";
			$obj_Output->name = "FileRepo";
			$obj_Output->date = date_format(new \DateTime(), "c");

			$str_URL = self::serverLookup("57631D2F-1F3B-4E9E-BC30-44233EFAF3A9");

			if ($str_URL == "")
			{
				$obj_Output->status = "DOWN";
				$obj_Output->msg = "FileRepo status url is not set (57631D2F-1F3B-4E9E-BC30-44233EFAF3A9)";
			}
			else
			{
				$obj_Rest = new cRestConnector($str_URL);

				$str_Return = $obj_Rest->send_Get();
				if (!$str_Return)
				{
					$obj_Output->status = "DOWN";
					$obj_Output->msg = $obj_Rest->getLastError();
				}
			}
			return $obj_Output;
		}

		protected function check_User_Manager()
		{
			$obj_Output = new \stdClass();
			$obj_Output->status = "UNKNOWN";

			$obj_Output->name = "User Manager";
			$obj_Output->date = date_format(new \DateTime(), "c");
			$obj_Output->msg = "Method has not been implemented.";
			return $obj_Output;
		}

		protected function check_AmpleHarvestDB()
		{
			$obj_Output = new \stdClass();
			$obj_Output->status = "UNKNOWN";

			$obj_Output->name = "AmpleHarvest DB";
			$obj_Output->date = date_format(new \DateTime(), "c");
			$obj_Output->msg = "Method has not been implemented.";
			return $obj_Output;
		}

		protected static function serverLookup($str_ServerID)
		{

			if (isset(cServers::$C_ARR_SERVERS[$str_ServerID]))
			{
				return cServers::$C_ARR_SERVERS[$str_ServerID];
			}
			return "";

		}
	}

	$obj_Service = new cStatus();

?>
